package cn.sciento.boot.tenant.hiam.app.service.impl;

import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import cn.sciento.boot.tenant.dto.TenantInitConfigDTO;
import cn.sciento.boot.tenant.dto.TenantInitLogDTO;
import cn.sciento.boot.tenant.hiam.app.service.HiamTenantInitService;
import cn.sciento.boot.tenant.hiam.domain.entity.TenantInitConfig;
import cn.sciento.boot.tenant.hiam.domain.entity.TenantInitLog;
import cn.sciento.boot.tenant.hiam.domain.repository.TenantInitConfigRepository;
import cn.sciento.boot.tenant.hiam.domain.repository.TenantInitLogRepository;
import cn.sciento.core.redis.RedisHelper;
import cn.sciento.mybatis.helper.SecurityTokenHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

@Service
public class HiamTenantInitServiceImpl implements HiamTenantInitService {
  private static final Logger LOGGER = LoggerFactory.getLogger(HiamTenantInitServiceImpl.class);
  
  @Autowired
  private RedisHelper redisHelper;
  
  @Autowired
  private Environment environment;
  
  @Autowired
  private TenantInitConfigRepository tenantInitConfigRepository;
  
  @Autowired
  private TenantInitLogRepository tenantInitLogRepository;
  
  @Override
  public TenantInitConfigDTO register(TenantInitConfigDTO tenantInitConfigDTO) {
    if (tenantInitConfigDTO == null) {
      return tenantInitConfigDTO;
    }
    TenantInitConfig tenantInitConfig = TenantInitConfig.initInstance(tenantInitConfigDTO);
    if (tenantInitConfig.isUpdate(this.tenantInitConfigRepository)) {
      this.tenantInitConfigRepository.updateByPrimaryKey(tenantInitConfig);
    } else {
      this.tenantInitConfigRepository.insertSelective(tenantInitConfig);
    } 
    tenantInitConfigDTO.doCache(this.redisHelper);
    return tenantInitConfigDTO;
  }
  
  @Override
  public TenantInitLogDTO log(TenantInitLogDTO tenantInitLogDTO) {
    if (tenantInitLogDTO == null) {
      return tenantInitLogDTO;
    }
    TenantInitLog tenantInitLog = new TenantInitLog();
    BeanUtils.copyProperties(tenantInitLogDTO, tenantInitLog);
    tenantInitLog.setProcessStatus(tenantInitLogDTO.getProcessStatus().name());
    tenantInitLog.setInitType(tenantInitLogDTO.getInitType().name());
    tenantInitLog.setProcessorType(tenantInitLogDTO.getProcessorType().name());
    this.tenantInitLogRepository.insertSelective(tenantInitLog);
    return tenantInitLogDTO;
  }
  
  @Override
  public void refreshTenantInitConfigCatch() {
    SecurityTokenHelper.close();
    List<TenantInitConfig> tenantInitConfigs = this.tenantInitConfigRepository.selectAll();
    TenantInitConfigDTO.clearCatch(this.redisHelper);
    if (CollectionUtils.isNotEmpty(tenantInitConfigs)) {
      tenantInitConfigs.forEach(item -> TenantInitConfigDTO.initInstance(item).doCache(this.redisHelper));
    }
  }
}
